package com.nice.controller;

import com.alibaba.fastjson.JSON;
import com.nice.dao.ObjectDao;
import com.nice.entity.PriceSchema;
import com.nice.entity.ResponseResult;
import com.nice.common.CacheUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

@Controller
public class PriceController {

    @Autowired
    ObjectDao objectDao;

    @PostMapping("/spotPrice")
    @ResponseBody
    public String spotPrice() {
        //获取内存牌价
        Map<String, PriceSchema> map = CacheUtils.priceInfo;
        //组装返回报文
        List<PriceSchema> list = new ArrayList<PriceSchema>();
        map.forEach(
                (k, v) -> {
                    list.add(v);
                }
        );
        //lambda表达式实现List接口sort方法排序
        list.sort((num1,num2)->{return num1.compareTo(num2);});
//        Collections.sort(list);
        return JSON.toJSONString(list);
    }



    @PostMapping("/selectPrice")
    @ResponseBody
    public String selectPrice() {
        //获取内存牌价
        Map<String,String> selectIndexNameMap =objectDao.selectIndexNameMap();
        Map<String, PriceSchema> map = CacheUtils.priceInfo;
        //组装返回报文
        List<PriceSchema> list = new ArrayList<PriceSchema>();
        if (selectIndexNameMap == null || selectIndexNameMap.isEmpty()||map == null || map.isEmpty()) {
            return JSON.toJSONString(list);
        }
        selectIndexNameMap.forEach(
                (d,f)->{
                    map.forEach(
                            (k, v) -> {
                                if (d.equals(k)) {
                                    list.add(v);
                                }

                            }
                    );
                }
        );

        //Collections.sort(list);
        //lambda表达式实现List接口sort方法排序
        list.sort((num1,num2)->{return num1.compareTo(num2);});

        return JSON.toJSONString(list);
    }

    @GetMapping("/spotPrice")
    @ResponseBody
    public String spotPrice1() {
        List<PriceSchema> list = new ArrayList<PriceSchema>();
        for (int i = 0; i < 10; i++) {
            PriceSchema priceSchema = new PriceSchema();
            priceSchema.setIndexName("index" + i);
            priceSchema.setAsk(String.valueOf(i * 2));
            priceSchema.setBid(String.valueOf(i * 2));
            list.add(priceSchema);
        }
        return JSON.toJSONString(list);
    }

    @PostMapping("/addSelect")
    @ResponseBody
    public String addSelect(@RequestParam String indexName) {
        //入文本库
        objectDao.insertSelectIndexNameMap(indexName);
        ResponseResult responseResult = new ResponseResult();
        responseResult.setResultCode(ResponseResult.SUCCESS);
        System.out.println("添加自选牌价成功:"+indexName);
        return JSON.toJSONString(responseResult);
    }

    @PostMapping("/removeSelect")
    @ResponseBody
    public String removeSelect(@RequestParam String indexName) {
//        CacheUtils.selectIndexName.remove(indexName);
        //刷下文本库
        objectDao.deleteSelectIndexNameMapByIndexName(indexName);
        ResponseResult responseResult = new ResponseResult();
        responseResult.setResultCode(ResponseResult.SUCCESS);
        System.out.println("移除自选牌价成功:"+indexName);
        return JSON.toJSONString(responseResult);
    }


    @PostMapping("/spotPriceById")
    @ResponseBody
    public String spotPriceById(@RequestParam String indexName) {
        //获取内存牌价
        Map<String, PriceSchema> map = CacheUtils.priceInfo;
        return JSON.toJSONString(map.get(indexName));
    }
}
